﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using vnFingerPrint.DataAccess;
using vnFingerPrint.BusinessObject;
using vnCommonGUI;
namespace UserControlForFingerprint
{
    public partial class CongDanMng : UserControl
    {
        string mode = "add";
        public void bindTinhThanh()
        {
            cbTinhThanh.DataSource = dmTinhThanh_DataAccess.SelectAll();
            cbTinhThanh.DisplayMember = "TenTinhThanh";
            cbTinhThanh.ValueMember = "Id";
        }

        public void bindQuanHuyen()
        {
            try
            {
                cbQuanHuyen.DataSource = dmQuanHuyen_DataAccess.SelectByIdTinhThanh((int)cbTinhThanh.SelectedValue);
                cbQuanHuyen.DisplayMember = "TenQuanHuyen";
                cbQuanHuyen.ValueMember = "Id";
                cbQuanHuyen.Enabled = true;
            }catch
            {
                cbQuanHuyen.Enabled = false;
            }
        }

        public void SetInfo(CongDan_Object co)
        {
            txtHoten.Text=co.HoTen;
            if(co.NgaySinh.HasValue)dateNgaySinh.Value=co.NgaySinh.Value;
            txtGioiTinh.Text=co.GioiTinh;
             txtSoCMND.Text=co.SoCMND;
            if (co.NgayCap.HasValue) dateNgayCap.Value = co.NgayCap.Value;
            txtNoiCapCMND.Text=co.NoiCap;
            txtPhuongXa.Text=co.PhuongXa;
            cbTinhThanh.SelectedIndex = cbTinhThanh.FindString((dmTinhThanh_DataAccess.GetObject(co.Id_TinhThanh.Value)).TenTinhThanh);
            cbQuanHuyen.SelectedIndex = cbQuanHuyen.FindString((dmQuanHuyen_DataAccess.GetObject(co.Id_QuanHuyen.Value)).TenQuanHuyen);
            txtGhiChu.Text=co.GhiChu;
            txtDiaChi.Text  = co.DiaChi;
            if (co.ImageUrl != "" && co.ImageUrl != null)
                pbChanDung.Image = new Bitmap(Config_DataAccess.GetCongDanImagePath(co.ImageUrl));
        }
        public void ClearInfo()
        {
            txtHoten.Text="";
            txtSoCMND.Text="";
            txtNoiCapCMND.Text="";
            txtPhuongXa.Text="";
            txtGhiChu.Text = "";
            txtDiaChi.Text = "";
            pbChanDung.Image = null;
        }
        public CongDanMng(string SoCMND, bool canEdit)
        {
            CongDan_Object co = CongDan_DataAccess.GetObject(SoCMND);
            if (!canEdit)
            {
                btnChangeImage.Enabled = false;
            }
            InitializeComponent();
        }
        public CongDanMng()
        {
            InitializeComponent();
            bindTinhThanh();
            bindQuanHuyen();
        }

        public void setMode(string _mode)
        {
            mode = _mode;
            txtSoCMND.Enabled = (mode == "add");
        }
        public bool validate()
        {
            if (txtHoten.Text == "")
            {
                MessageBox.Show("Quí vị phải nhập họ tên!");
                txtHoten.Focus();
                return false;
            }
            if (txtSoCMND.Text == "")
            {
                MessageBox.Show("Quí vị phải nhập Số CMND!");
                txtSoCMND.Focus();
                return false;
            }
            return true;
        }
        public CongDan_Object GetInfo()
        {
            if (!validate()) return null;
            CongDan_Object newco = new CongDan_Object();
            newco.HoTen = txtHoten.Text;
            newco.NgaySinh = dateNgaySinh.Value;
            newco.GioiTinh = txtGioiTinh.Text;
            newco.SoCMND = txtSoCMND.Text;
            newco.NgayCap = dateNgayCap.Value;
            newco.NoiCap = txtNoiCapCMND.Text;
            newco.PhuongXa = txtPhuongXa.Text;
            newco.SoHoChieu = "";
            newco.DiaChi = txtDiaChi.Text;
            if (cbQuanHuyen.SelectedValue != null)
                newco.Id_QuanHuyen = (int)cbQuanHuyen.SelectedValue;
            newco.Id_TinhThanh = (int)cbTinhThanh.SelectedValue;
            newco.GhiChu = txtGhiChu.Text;
            if (pbChanDung.Image == null) newco.ImageUrl = "";
            else newco.ImageUrl = newco.SoCMND + ".jpg";
            return newco;
        }
        public void SaveImage()
        {
            if (txtSoCMND.Text  != "") ;
            pbChanDung.Image.Save(Config_DataAccess.GetCongDanImagePath(txtSoCMND.Text+".jpg"));
        }
        private void cbTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindQuanHuyen();
        }

        private void btnChangeImage_Click(object sender, EventArgs e)
        {
            pbChanDung.Image = CommonFunction.OpenDialogImageFile();
        }

        private void CongDanMng_Load(object sender, EventArgs e)
        {

        }
    }
}
